﻿@page "/BusinessDataDisplay"
@using   ChartJs.Blazor.Common.Axes.Ticks
@using   ChartJs.Blazor.Common.Enums
@using   ChartJs.Blazor.Common.Handlers
@using   ChartJs.Blazor.Common.Time
@using   ChartJs.Blazor.Util
@using   ChartJs.Blazor.Interop
@using PieConfig = ChartJs.Blazor.PieChart.PieConfig
@using PieOptions = ChartJs.Blazor.PieChart.PieOptions
@using OptionsTitle = ChartJs.Blazor.Common.OptionsTitle
@using ColorUtil = ChartJs.Blazor.Util.ColorUtil
@inject ChangeServer changeServer
    <Col  Span="24" Lg="8" Md="12" >
    <ChartJs.Blazor.Chart Config="_config"></ChartJs.Blazor.Chart>
    </Col>
    @code
{
        [Parameter]
        public ObservableCollection<ChangeEntity> Changes { get; set; } = new ObservableCollection<ChangeEntity>();
        [Parameter]
        public string Title { get; set; } = "ChartJs.Blazor Pie Chart";
        private PieConfig _config;
        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        int tt = 0;

        protected override Task OnAfterRenderAsync(bool firstRender)
        {
            if (Changes != null && Changes.Count != 0)
            {
                a = Changes.Where(x => x.State == (int)State.Unoccupied && !string.IsNullOrEmpty(x.MaterialName)).Count();
                b = Changes.Where(x => x.State == (int)State.Tobetransported).Count();
                c = Changes.Where(x => x.State == (int)State.Unoccupied && string.IsNullOrEmpty(x.MaterialName)).Count();
                d = Changes.Where(x => x.State == (int)State.Tobeshippedout).Count();
                e = Changes.Where(x => x.State == (int)State.locking).Count();
            }
            ChartJs.Blazor.PieChart.PieDataset<int> dataset = new ChartJs.Blazor.PieChart.PieDataset<int>(new[] { a, b, c, d, e })
            {
                BackgroundColor = new[]
                {
                ColorUtil.ColorHexString(255, 99, 132), // Slice 1 aka "Red"
                ColorUtil.ColorHexString(255, 205, 86), // Slice 2 aka "Yellow"
                ColorUtil.ColorHexString(75, 192, 192), // Slice 3 aka "Green"
                ColorUtil.ColorHexString(54, 162, 235), // Slice 4 aka "Blue"
                ColorUtil.ColorHexString(200, 162, 235), // Slice 4 aka "Blue"
               }
            };
            tt++;

            _config.Data.Datasets.Clear();
            _config.Data.Datasets.Add(dataset);
            return base.OnAfterRenderAsync(firstRender);
        }

        protected override void OnParametersSet()
        {

            //if (Changes != null && Changes.Count != 0)
            //{
            //    a = Changes.Where(x => x.State == (int)State.Unoccupied && x.MaterialType != 0).Count();
            //    b = Changes.Where(x => x.State == (int)State.Tobetransported).Count();
            //    c = Changes.Where(x => x.State == (int)State.Unoccupied && x.MaterialType == 0).Count();
            //    d = Changes.Where(x => x.State == (int)State.Tobeshippedout).Count();
            //    e = Changes.Where(x => x.State == (int)State.locking).Count();
            //}
            //ChartJs.Blazor.PieChart.PieDataset<int> dataset = new ChartJs.Blazor.PieChart.PieDataset<int>(new[] { a, b, c, d, e })
            //{
            //    BackgroundColor = new[]
            //    {
            //        ColorUtil.ColorHexString(255, 99, 132), // Slice 1 aka "Red"
            //        ColorUtil.ColorHexString(255, 205, 86), // Slice 2 aka "Yellow"
            //        ColorUtil.ColorHexString(75, 192, 192), // Slice 3 aka "Green"
            //        ColorUtil.ColorHexString(54, 162, 235), // Slice 4 aka "Blue"
            //        ColorUtil.ColorHexString(200, 162, 235), // Slice 4 aka "Blue"
            //       }
            //};
            //tt++;

            //_config.Data.Datasets.Clear();
            //_config.Data.Datasets.Add(dataset);
            base.OnParametersSet();
        }


        //protected override async Task OnParametersSetAsync()
        //{
        //    //_config = new PieConfig
        //    //{
        //    //    Options = new PieOptions
        //    //    {
        //    //        Responsive = true,
        //    //        Title = new OptionsTitle
        //    //        {
        //    //            Display = true,
        //    //            Text = Title,
        //    //        }
        //    //    }
        //    //};
        //    //foreach (string color in new[] { "占用", "待运达", "空闲", "待运出", "人为锁定" })
        //    //{
        //    //    _config.Data.Labels.Add(color);
        //    //}
        //    if (Changes != null && Changes.Count != 0)
        //    {
        //        a = Changes.Where(x => x.State == (int)State.Unoccupied && x.MaterialType != 0).Count();
        //        b = Changes.Where(x => x.State == (int)State.Tobetransported).Count();
        //        c = Changes.Where(x => x.State == (int)State.Unoccupied && x.MaterialType == 0).Count();
        //        d = Changes.Where(x => x.State == (int)State.Tobeshippedout).Count();
        //        e = Changes.Where(x => x.State == (int)State.locking).Count();
        //    }
        //    ChartJs.Blazor.PieChart.PieDataset<int> dataset = new ChartJs.Blazor.PieChart.PieDataset<int>(new[] { a, b, c, d, e })
        //    {
        //        BackgroundColor = new[]
        //        {
        //        ColorUtil.ColorHexString(255, 99, 132), // Slice 1 aka "Red"
        //        ColorUtil.ColorHexString(255, 205, 86), // Slice 2 aka "Yellow"
        //        ColorUtil.ColorHexString(75, 192, 192), // Slice 3 aka "Green"
        //        ColorUtil.ColorHexString(54, 162, 235), // Slice 4 aka "Blue"
        //        ColorUtil.ColorHexString(200, 162, 235), // Slice 4 aka "Blue"
        //       }
        //    };
        //    _config.Data.Datasets.Clear();
        //    _config.Data.Datasets.Add(dataset);
        //    await this.InvokeAsync(() => { this.StateHasChanged(); });
        //    await  base.OnParametersSetAsync();
        //}

        protected override async Task OnInitializedAsync()
        {


            _config = new PieConfig
            {
                Options = new PieOptions
                {
                    Responsive = true,
                    Title = new OptionsTitle
                    {
                        Display = true,
                        Text = Title,
                    }
                }
            };
            foreach (string color in new[] { "占用", "待运达", "空闲", "待运出", "人为锁定" })
            {
                _config.Data.Labels.Add(color);
            }
            if (Changes != null && Changes.Count != 0)
            {
                a = Changes.Where(x => x.State == (int)State.Unoccupied && !string.IsNullOrEmpty(x.MaterialName)).Count();
                b = Changes.Where(x => x.State == (int)State.Tobetransported).Count();
                c = Changes.Where(x => x.State == (int)State.Unoccupied && string.IsNullOrEmpty(x.MaterialName)).Count();
                d = Changes.Where(x => x.State == (int)State.Tobeshippedout).Count();
                e = Changes.Where(x => x.State == (int)State.locking).Count();


            }
            ChartJs.Blazor.PieChart.PieDataset<int> dataset = new ChartJs.Blazor.PieChart.PieDataset<int>(new[] { a, b, c, d, e })
            {
                BackgroundColor = new[]
            {
            ColorUtil.ColorHexString(255, 99, 132), // Slice 1 aka "Red"
            ColorUtil.ColorHexString(255, 205, 86), // Slice 2 aka "Yellow"
            ColorUtil.ColorHexString(75, 192, 192), // Slice 3 aka "Green"
            ColorUtil.ColorHexString(54, 162, 235), // Slice 4 aka "Blue"
            ColorUtil.ColorHexString(200, 162, 235), // Slice 4 aka "Blue"
           }
            };

            _config.Data.Datasets.Add(dataset);

            //_config = new PieConfig
            //{
            //    Options = new PieOptions
            //    {
            //        Responsive = true,
            //        Title = new OptionsTitle
            //        {
            //            Display = true,
            //            Text = "ChartJs.Blazor Pie Chart"
            //        }
            //    }
            //};

            //foreach (string color in new[] { "占用", "待运达", "空闲", "待运出", "人为锁定" })
            //{
            //    _config.Data.Labels.Add(color);
            //}
            //var list = await changeServer.GetChanges();
            //int a = 0;
            //int b = 0;
            //int c = 0;
            //int d = 0;
            //int e = 0;
            //if (list != null)
            //{
            //    a = list.Where(x => x.State == (int)State.Unoccupied).Count();
            //    b = list.Where(x => x.State == (int)State.Unoccupied).Count();
            //    c = list.Where(x => x.State == (int)State.Unoccupied).Count();
            //    d = list.Where(x => x.State == (int)State.Unoccupied).Count();
            //    e = list.Where(x => x.State == (int)State.Unoccupied).Count();
            //}
            //ChartJs.Blazor.PieChart.PieDataset<int> dataset = new ChartJs.Blazor.PieChart.PieDataset<int>(new[] { a, b, c, d, e })
            //{
            //    BackgroundColor = new[]
            //{
            //    ColorUtil.ColorHexString(255, 99, 132), // Slice 1 aka "Red"
            //    ColorUtil.ColorHexString(255, 205, 86), // Slice 2 aka "Yellow"
            //    ColorUtil.ColorHexString(75, 192, 192), // Slice 3 aka "Green"
            //    ColorUtil.ColorHexString(54, 162, 235), // Slice 4 aka "Blue"
            //    ColorUtil.ColorHexString(200, 162, 235), // Slice 4 aka "Blue"
            //}
            //};

            //_config.Data.Datasets.Add(dataset);



            //_config = new PieConfig
            //{
            //    Options = new PieOptions
            //    {
            //        Responsive = true,
            //        Title = new OptionsTitle
            //        {
            //            Display = true,
            //            Text = "ChartJs.Blazor Pie Chart"
            //        }
            //    }
            //};
            //foreach (string color in new[] { "占用", "待运达", "空闲", "待运出", "人为锁定" })
            //{
            //    _config.Data.Labels.Add(color);
            //}
            //if (Changes != null)
            //{
            //    a = Changes.Where(x => x.State == (int)State.Unoccupied && x.MaterialType != 0).Count();
            //    b = Changes.Where(x => x.State == (int)State.Tobetransported).Count();
            //    c = Changes.Where(x => x.State == (int)State.Unoccupied && x.MaterialType == 0).Count();
            //    d = Changes.Where(x => x.State == (int)State.Tobeshippedout).Count();
            //    e = Changes.Where(x => x.State == (int)State.locking).Count();
            //}
            //ChartJs.Blazor.PieChart.PieDataset<int> dataset = new ChartJs.Blazor.PieChart.PieDataset<int>(new[] { a, b, c, d, e })
            //{
            //    BackgroundColor = new[]
            //   {
            //    ColorUtil.ColorHexString(255, 99, 132), // Slice 1 aka "Red"
            //    ColorUtil.ColorHexString(255, 205, 86), // Slice 2 aka "Yellow"
            //    ColorUtil.ColorHexString(75, 192, 192), // Slice 3 aka "Green"
            //    ColorUtil.ColorHexString(54, 162, 235), // Slice 4 aka "Blue"
            //    ColorUtil.ColorHexString(200, 162, 235), // Slice 4 aka "Blue"
            //   }
            //};

            //_config.Data.Datasets.Add(dataset);
            base.OnInitialized();
        }
    }


